﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.ObjectModel;
using SurveillanceLib;
using CommonClass;
using DAL;


namespace EnvCommonUtility
{
    /// <summary>
    /// 树的操作
    /// </summary>
    public class EnvTreeViewOperation
    {
        #region 属性

        /// <summary>
        /// 设备断开连接处理
        /// </summary>
        public Action<int, StringBuilder, int, IntPtr> DisConnectAction { get; set; }

        #endregion

        #region 设备树

        /// <summary>
        /// 获得数据库信息放入 TreeView 中(递归)--设备树
        /// </summary>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <param name="parentID">过滤 ID</param>
        /// <param name="currentNode">本结点</param>
        /// <param name="parentNode">父结点</param>
        public void AddDeviceTreeView(int parentID, TreeGroupModel currentNode, TreeGroupModel parentNode,
            string connectionStringName = null, string mainProjectEXE = null)
        {
            try
            {
                SurveillanceDAL dataOper = new SurveillanceDAL();
                DataView dataTree = dataOper.GetDataSet(string.Format(SurveillanceDAL.QUERY_GET_TREENODEDATA_BYSTATUS, 1), null,
                    connectionStringName, mainProjectEXE).Tables[0].DefaultView;
                dataTree.RowFilter = string.Format("ParentID = {0}", parentID);

                foreach (DataRowView row in dataTree)
                {
                    TagModel tag = new TagModel()
                    {
                        Driver_IsLogin = false,
                        Driver_ExceptChannel = row["ExceptChannel"].ToString(),
                        Driver_IP = row["TreeName"].ToString(),
                        Driver_Port = row["DriverPort"].ToString(),
                        Driver_User = row["DriverName"].ToString(),
                        Driver_Password = row["DriverPwd"].ToString(),
                        Driver_Type = row["DriverType"].ToString(),
                        Driver_Instance = DriverFactory.Factory.CreateDriver(row["DriverType"].ToString()),
                    };

                    TreeGroupModel node = new TreeGroupModel(row["TreeText"].ToString(), new ObservableCollection<ITreeGroup>())
                    {
                        Arg = tag,
                        Parent = parentNode,
                        TooltipText = string.IsNullOrWhiteSpace(row["DriverPort"].ToString()) ? 
                            string.Format("{0}", row["TreeText"].ToString()) :
                            string.Format("{0}[{1}:{2}]", row["TreeText"].ToString(),
                                row["TreeName"].ToString(), row["DriverPort"].ToString()),
                        Level = parentNode == null ? 0 : parentNode.Level + 1, // 0: 根
                    };

                    if (tag != null && tag.Driver_Instance != null &&
                        this.DisConnectAction != null)
                    {
                        tag.Driver_Instance.InitializeData(this.DisConnectAction, IntPtr.Zero); // 初始化状态
                        tag.Driver_Instance.SetEncoding("gb2312"); // 字符编码格式设置，默认为 gb2312 字符编码
                    }

                    currentNode.Leaves.Add(node);
                    AddDeviceTreeView(Int32.Parse(row["ID"].ToString().Trim()), node, node,
                        connectionStringName, mainProjectEXE);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion

        #region 区域树

        /// <summary>
        /// 获得数据库信息放入 TreeView 中(递归)--区域树
        /// </summary>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <param name="parentID">过滤 ID</param>
        /// <param name="currentNode">本结点</param>
        /// <param name="parentNode">父结点</param>
        public void AddRegionTreeView(int parentID, TreeGroupModel currentNode, TreeGroupModel parentNode,
            string connectionStringName = null, string mainProjectEXE = null)
        {
            try
            {
                ISSDal dataOper = new ISSDal();
                DataView dataTree = dataOper.GetDataSet(ISSDal.QUERY_GET_ALLREGIONDATA, null,
                    connectionStringName, mainProjectEXE).Tables[0].DefaultView;
                dataTree.RowFilter = string.Format("RegionParent = {0}", parentID);

                foreach (DataRowView row in dataTree)
                {
                    ArgModel arg = new ArgModel()
                    {
                        sKey = row["RegionID"].ToString(),
                        sValue = row["RegionURL"].ToString(),
                    };

                    TreeGroupModel node = new TreeGroupModel(row["RegionName"].ToString(), new ObservableCollection<ITreeGroup>())
                    {
                        Arg = arg,
                        Parent = parentNode,
                        TooltipText = row["RegionName"].ToString(),
                        Level = parentNode == null ? 0 : parentNode.Level + 1, // 0: 根
                    };

                    currentNode.Leaves.Add(node);
                    AddRegionTreeView(Int32.Parse(row["RegionID"].ToString().Trim()), node, node,
                        connectionStringName, mainProjectEXE);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}

